﻿#include "../head/CheckingAccount.hh"
#include "../head/tools.hh"
#include <iostream>
#include <iomanip>

CheckingAccount::CheckingAccount(std::string accountId, long long  balance, std::string holder) : Account(accountId, balance, holder) {}

bool CheckingAccount::deposit(long long  amount, const std::string& note) {
    if (amount > 0) {
        balance += amount;
        std::ostringstream oss;
        oss << "Deposit:     ￥" << std::setw(46) << std::left << tools::amountdisplay(amount) << "   Note: " << note;
        recordTransaction(oss.str());
        return 1;
    }
    return 0;
}

bool CheckingAccount::withdraw(long long  amount, const std::string& note) {
    if (amount > 0 && balance >= amount) {
        balance -= amount;

        std::ostringstream oss;
        oss << "Withdraw:    ￥" << std::setw(46) << std::left << tools::amountdisplay(amount) << "   Note: " << note;
        recordTransaction(oss.str());
        return 1;
    }
    return 0;
}

void CheckingAccount ::showinfo() {
    std::cout << "===== Bank Account: " << accountId << " (Checking Account) =====\n";
    std::cout << "Account holder: " << accountHolder << std::endl;
    std::cout << "Balance: ￥" << tools::amountdisplay(balance) << "\n";
}

std::string CheckingAccount::generateAccountID()
{
    std::string accountId;
    // 账户类型：21 - 储蓄账户，22 - 支票账户
    accountId += "6222";
    // 生成四位分支代码
    accountId += "-" + tools::generateRandomNumber(4);
    // 生成八位账户号（分成两部分：ZZZZ-AAAA）
    accountId += "-" + tools::generateRandomNumber(4) + "-" + tools::generateRandomNumber(4);
    return accountId;
}
